package com.info.game;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.component.game.Component;
import com.component.game.RenderComponent;
import com.engine.game.GameFabric;
import com.engine.game.GameObject;
import com.engine.game.MessageInfo;

public class SquareRenderComponent implements RenderComponent{
	
	private Sprite sprite;
	
	public enum Color{
		blue,
		yellow,
		green,
		brown,
		purpule,
		red,
		counter
	}
	
	public Color currentColor;
	
	private boolean wasChecked = false;
	
	private int id = -1;
	
	private Vector2 currentCoord = new Vector2();
	private Vector2 currentSize = new Vector2();
	
	public SquareRenderComponent(){
		
		
	
		String[] images = {"BlueSquare.png", "BrownSquare.png", "GreenSquare.png", "PurpuleSquare.png",
				"RedSquare.png", "YellowSquare.png"};
		
		ArrayList<String> imgList = new ArrayList<String>(Arrays.asList(images));
		
		Color[] colors = {Color.blue, Color.brown, Color.green, Color.purpule, Color.red, Color.yellow};
		
		ArrayList<Color> colorList = new ArrayList<Color>(Arrays.asList(colors));
		
		
		
		long seed = System.nanoTime();
		Collections.shuffle(imgList, new Random(seed));
		Collections.shuffle(colorList, new Random(seed));
		
		this.sprite = GameFabric.createSprite(imgList.get(0));
		this.currentColor = colorList.get(0);
		
		this.currentCoord.x = 0.0f;
		this.currentCoord.y = 0.0f;
		
		this.currentSize.x =  this.sprite.getWidth();
		this.currentSize.y = this.sprite.getHeight();
		
		this.sprite.setBounds(0.0f, 0.0f, this.sprite.getWidth(), this.sprite.getHeight());
	}

	@Override
	public void dispose() {
		this.sprite.getTexture().dispose();
		
	}

	@Override
	public void update(GameObject object) {
		
		 for(MessageInfo message : object.getRenderMessages()){
			 
			 
			 if(message.message.contains("set_id")){
				 String[] parts = message.message.split(";");
				 
				 this.id = Integer.parseInt(parts[1]);
			 }
			 else if(message.message.contains("set_bounds")){
				 String[] parts = message.message.split(";");
		//		 System.out.println(parts[1] + " " + parts[2] + " " + parts[3] + " " + parts[4] + " " + this.id);
				 this.currentCoord.x = Float.parseFloat(parts[1]);
				 this.currentCoord.y = Float.parseFloat(parts[2]);
				 this.currentSize.x = Float.parseFloat(parts[3]);
				 this.currentSize.y = Float.parseFloat(parts[4]);
				 
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
			 }
			 else if(message.message.contains("set_color")){
				 
				 String[] parts = message.message.split(";");
				 
				 this.setColor(parts[1]);
				 
				 
			 }
			 else if(message.message.contains("set_unchecked")){
				 this.wasChecked = false;
			 }
			 else if(message.message.contains("spread")){
				 
				 String[] parts = message.message.split(";");
				 
				 if(this.id == 1){
					 
					 this.spread(object, parts[1], this.currentColor.name());
					 
					 this.setColor(parts[1]);
					 
					 
					 this.wasChecked = true;
				 }
				 else{
				 
				 if(!(this.wasChecked)){
					 
					 if(parts[2].equals(this.currentColor.name())){
						 this.spread(object, parts[1], this.currentColor.name());
						 
						 this.setColor(parts[1]);
					 }
					 
					 this.wasChecked = true;
				
				 }
				 
				 
				 }
			 }
			 
		 }
		 
		 if(this.id > 0){
			 LabelAIComponent.colors.put(this.id, this.currentColor);
		//	 System.out.println("cool");
		 }
		 
	}
	
	public void spread(GameObject object, String newColor, String oldColor){
		
		
		if(((this.id - 1) != 0) && ((this.id - 1) % 14 != 0 )){
			object.generate("spread;" + newColor + ";" + oldColor, "SQUARE_" + Integer.toString(this.id - 1), Component.Render);
		}
		 
		 
		if(this.id % 14 != 0 ){
			object.generate("spread;" + newColor + ";" + oldColor, "SQUARE_" + Integer.toString(this.id + 1), Component.Render);
		}
		 
		 
		 
		if((this.id + 14) <= 196 ){
			object.generate("spread;" + newColor + ";" + oldColor, "SQUARE_" + Integer.toString(this.id + 14), Component.Render);
		}

		 
		 
		if((this.id - 14) > 0 ){
			object.generate("spread;" + newColor + ";" + oldColor, "SQUARE_" + Integer.toString(this.id - 14), Component.Render);
		}
		 
	}
	
	
	public void setColor(String color){
		
		Color c = Color.valueOf(color);
		
		switch(c){
		 
		 case blue :
			 
			 if(!(this.currentColor.equals(Color.blue))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("BlueSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.blue;
			 }
			
			 break;
			 
		 case brown :
			 if(!(this.currentColor.equals(Color.brown))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("BrownSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.brown;
			 }
			 break;
			 
		 case green :
			 if(!(this.currentColor.equals(Color.green))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("GreenSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.green;
			 }
			 break;
			 
		 case purpule :
			 if(!(this.currentColor.equals(Color.purpule))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("PurpuleSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.purpule;
			 }
			 break;
			 
		 case red :
			 if(!(this.currentColor.equals(Color.red))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("RedSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.red;
			 }
			 break;
			 
		 case yellow :
			 if(!(this.currentColor.equals(Color.yellow))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("YellowSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.yellow;
			 }
			 break;
			 
		 case counter :
			 if(!(this.currentColor.equals(Color.counter))){
				 this.sprite.getTexture().dispose();
				 this.sprite = GameFabric.createSprite("CounterSquare.png");
				 this.sprite.setBounds(this.currentCoord.x, this.currentCoord.y, this.currentSize.x, this.currentSize.y);
				 this.currentColor = Color.counter;
			 }
			 break;
			 
			 
		 
		 }
	}

	@Override
	public Sprite getSprite() {
		return this.sprite;
	}

}
